package com.yl.androidutilscenter.http

import com.yl.androidutilscenter.utils.ext.toStr
import okhttp3.FormBody
import okhttp3.MultipartBody
import okhttp3.Request
import okio.Buffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets

/**
 * @author
 * @description:
 * @date :2024/3/28
 * @version 1.0.0
 */
object ParamsUtils {

    /**
     * 获取网络请求的请求参数
     */
     fun getRequestParams(request: Request): String {
        if (request.method() == "GET") {
            return request.url().encodedQuery().toStr("")
        } else if (request.method() == "POST") {
            request.body()?.let {
                if (it is FormBody) {//表单传递的数据
                    val buffer = Buffer()
                    try {
                        it.let {
                            it.writeTo(buffer)
                            var charset = Charset.forName("UTF-8")
                            val contentType = it.contentType()
                            charset = contentType?.charset(StandardCharsets.UTF_8)
                            return buffer.readString(charset)
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } else if (it is MultipartBody) {
                    val params = mutableMapOf<String, String>()
                    val files = mutableMapOf<String, String>()
                    it.parts().forEach { part ->
                        val body = part.body()
                        part.headers()?.let {headers->
                            val header = headers.value(0)
                            val split = header.replace(" ", "").replace("\"", "").split(";")
                            when (split.size) {
                                2 -> {
                                    //文本参数
                                    val keys = split[1].split("=")
                                    if (keys.size > 1 && body.contentLength() < 1024) {
                                        val key = keys[1]
                                        val buffer = Buffer()
                                        body.writeTo(buffer)
                                        val value = buffer.readUtf8()
                                        params[key] = value
                                    }
                                }

                                3 -> {
                                    //文件
                                    val fileKeys = split[1].split("=")
                                    val fileKey = if (fileKeys.size > 1) {
                                        fileKeys[1]
                                    } else ""
                                    val nameValue = split[2].split("=")
                                    val fileName = if (nameValue.size > 1) nameValue[1] else ""
                                    files[fileKey] = fileName
                                }
                            }
                        }
                    }
                    val strParams = StringBuffer()
                    params.forEach { map ->
                        strParams.append("${map.key}=${map.value}&")
                    }
                    return strParams.substring(0, strParams.toString().length - 1)
                }
            }
            return ""
        }
        return ""
    }
}